import utils from "./utils";

const part = {
    regex: /回填|过渡|90区|92区|93区|94区|95区|96区/,
    transferData: (item, maxHeight, dataType) => {
        let s_width = item.shape_type === 'circle' ? item.length : item.length;
        let s_height = item.shape_type === 'circle' ? item.length : item.width;
        let obj = $.extend({}, item);
        obj.style = `top: ${item.position_y}px;left: ${item.position_x}px;width: ${s_width}px;height: ${s_height}px;line-height: ${s_height}px;`;
        if (item.position_y + s_height > maxHeight) {
            maxHeight = item.position_y + s_height;
        }
        obj.PartsInfo = {};
        obj.maxHeight = maxHeight;
        return obj;
    },
    convertData: (list) => {
        let qdmouterWidth = 0, ssdLength = 0;
        list.forEach(data => {
            if (data.enginExtList) {
                let currssdLen = data.enginExtList.length;
                ssdLength += currssdLen > 2 ? currssdLen : 1;
                let sectionDetails = [];
                data.enginExtList.forEach(item => {
                    let zfMaxHeight = 0, yfMaxHeight = 0;
                    if (item.partList) {
                        let dataList = [];
                        item.partList.forEach(item2 => {
                            if (item2.base_width !== 462) {
                                item2.width = utils.getScaleResult(item2.width, item2.base_width, 460);
                                item2.length = utils.getScaleResult(item2.length, item2.base_width, 460);
                                item2.position_x = utils.getScaleResult(item2.position_x, item2.base_width, 460);
                                item2.position_y = utils.getScaleResult(item2.position_y, item2.base_width, 460);
                                item2.base_width = 462;
                            }
                            let obj = part.transferData(item2, (item.position_type === 'yf' ? yfMaxHeight : zfMaxHeight));
                            if (item.position_type === 'yf' && yfMaxHeight < obj.maxHeight) {
                                yfMaxHeight = obj.maxHeight;
                            } else {
                                if (zfMaxHeight < obj.maxHeight) {
                                    zfMaxHeight = obj.maxHeight;
                                }
                            }
                            dataList.push(obj);
                        });
                        item.partList = dataList;
                    }
                    zfMaxHeight += 20;
                    yfMaxHeight += 20;
                    item.maxHeight = {
                        zfMaxHeight, yfMaxHeight
                    };
                    if (data.engin_type === 'ql') {
                        item.dm_name = item.parent_name;
                    } else {
                        item.dm_name = data.name;
                    }
                    sectionDetails.push($.extend({}, item));
                });
                data.enginExtList = [].concat(sectionDetails);
            }
        });
        let zfMaxHeight = 0, yfMaxHeight = 0;
        list.forEach(data => {
            if (data.enginExtList) {
                data.enginExtList.forEach(item => {
                    if (item.maxHeight) {
                        if (zfMaxHeight < item.maxHeight.zfMaxHeight) {
                            zfMaxHeight = item.maxHeight.zfMaxHeight;
                        }
                        if (yfMaxHeight < item.maxHeight.yfMaxHeight) {
                            yfMaxHeight = item.maxHeight.yfMaxHeight;
                        }
                    }
                });
            }
        });
        list.forEach(data => {
            if (data.enginExtList) {
                data.enginExtList.forEach(item => {
                    if (item.position_type === 'yf') {
                        item.qdmHeight = yfMaxHeight;
                    } else {
                        item.qdmHeight = zfMaxHeight;
                    }
                });
            }
        });
        qdmouterWidth= ssdLength * 466;
        return {qdmouterWidth, list};
    },
    getPositionType (siteItem) {
        if (siteItem.position_type === 'zf') {
            return '左';
        } else if (siteItem.position_type === 'yf') {
            return '右';
        } else if (siteItem.position_type === 'qf') {
            return '全';
        }
        return '';
    },
    initCheckedData(datas, isAll) {
        let list = [];
        if (datas && datas.length > 0) {
            datas.forEach(item => {
                if (item.data_type === 'dm') {
                    if (item.partList && item.partList.length > 0) {
                        let filter = item.partList.filter(p => p.is_checked);
                        if (isAll) {
                            filter = item.partList;
                        }
                        if (filter.length > 0) {
                            filter.forEach(p => {
                                item.position_name1 = part.getPositionType(item);
                                p.engin = item;
                                list.push(p);
                            });
                        }
                    }
                } else {
                    if (item.enginExtList && item.enginExtList.length > 0) {
                        let childs = part.initCheckedData(item.enginExtList, isAll);
                        if (childs.length > 0) {
                            childs.forEach(p => {
                                list.push(p);
                            });
                        }
                    }
                }
            });
        }
        return list;
    },
    getEnginMileage(engin) {
        if (!utils.isNotEmpty(engin.mileage)) {
            let value1 = utils.accSubtr((engin.mileage_x2 || 0), (engin.mileage_x1 || 0));
            let value2 = utils.accSubtr((engin.mileage_y2 || 0), (engin.mileage_y1 || 0));
            let mileage = utils.accMul(value1, 1000);
            return utils.accAdd(mileage, value2);
        }
        return engin.mileage;
    }
};
export default part;
